package com.wzy.Controller;


import com.wzy.entity.Comments;
import com.wzy.service.BlogService;
import com.wzy.service.CommentsService;
import com.wzy.service.HeroService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 评论控制层
 * </p>
 *
 * @author 杨欢
 * @since 2021-02-25
 */
@Controller
public class CommentsController {


    @Autowired
    private CommentsService commentsService;
    @Autowired
    private BlogService blogService;
    @Autowired
    private HeroService heroService;

    //    从配置中拿到评论头像
    @Value("${comments.img}")
    private String commentImg;

    //    跳转到博客详情页面中th:fragment=commentList的标签
    @GetMapping("/comments/{blogId}")
    public String comments(@PathVariable("blogId") long blogId, Model model){
        System.out.println("blog评论查询");
        model.addAttribute("comments",this.commentsService.listCommentsByBlogId(blogId,(long)0));
        return "detail :: commentList";
    }


    //    跳转到英雄博客详情页面中th:fragment=commentList的标签
    @GetMapping("/heroComments/{heroId}")
    public String heroComments(@PathVariable("heroId") long heroId, Model model){
        System.out.println("hero评论查询");
        model.addAttribute("comments",this.commentsService.listCommentsByBlogId((long)0,heroId));
        return "detail :: commentList";
    }

    //    新增评论
    @PostMapping("/comments")
    public String post(Comments comments, HttpSession session){
//        这里是判断 当前博客中评论的人是否是站长，是|不是 进行不同的处理
        long blogId=comments.getBlogId();
        comments.setBlog(this.blogService.detail(blogId));
//        Users user=(Users) session.getAttribute("user");
//        if(user!=null){
//            comments.setImg(user.getImg());
//            comments.setAdmin_comment(true);
//        }else {
            comments.setImg(commentImg);
//        }
        this.commentsService.append(comments);
        return "redirect:/comments/"+blogId;
    }

    @PostMapping("/heroComments")
    public String post1(Comments comments, HttpSession session){
//        这里是判断 当前博客中评论的人是否是站长，是|不是 进行不同的处理
        long heroId=comments.getHeroId();
        comments.setHero(this.heroService.detail(heroId));
//        Users user=(Users) session.getAttribute("user");
//        if(user!=null){
//            comments.setImg(user.getImg());
//            comments.setAdmin_comment(true);
//        }else {
        comments.setImg(commentImg);
//        }
        this.commentsService.append(comments);
        return "redirect:/heroComments/"+heroId;
    }
}

